//
//  UITableViewCell+HDChatListCell.m
//  app
//
//  Created by D on 15/5/18.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDChatListCell.h"
#import "HDChatListManager.h"

static const CGFloat kMargin = 24.0f;
static const CGFloat kFaceHight = 92.0f;
static const CGFloat kNameWight = 180.0f;
static const CGFloat kTalkHight = 40.0f;
static const CGFloat kTalkWight = 320.0f;
static const CGFloat kTimeWight = 156.0f;
static const CGFloat kTimeHight = 40.0f;
static const CGFloat kRedPointH = 20.0f;

@interface HDChatListCell ()

@property (nonatomic,strong) HDChatListManager * listManger;

@end

@implementation HDChatListCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier userId:(NSString*)uid
{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.uid = uid;
        self.listManger = [HDChatListManager shareManager];
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addCellRedPoint:) name:@"cellRedPoint" object:nil];

   
    }
    return self;
}

-(void)cellWithModel:(HDChatListModel*)model withTempPmid:(NSMutableArray*)tempPmids
{
//    DLog(@"temp = %@",tempPmids);
    
    
        
        HDAppDelegate *appDelegate = (HDAppDelegate *)[UIApplication sharedApplication].delegate;
        self.frame = CGRectMake(0, 0, appDelegate.window.frame.size.width, UF(132));

        self.pmid = model.pmid;
        self.fuid = model.uid;
        self.fName = model.name;
        self.faceurl = model.face;
        
        
        UIImage * BGimage = [UIImage imageNamed:@"head_144.png"];
        CGFloat top = 6;
        CGFloat bottom = 6;
        CGFloat left = 6;
        CGFloat right = 6;
        UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
        BGimage = [BGimage resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
        
        
        
        
        self.face = [[UIImageView alloc]initWithFrame:CGRectOf([self.ubo ux:kMargin fy:UB_1_2 uy:-kFaceHight/2], [self.ubo uw:kFaceHight uh:kFaceHight])];
        self.face.layer.masksToBounds = YES;
        self.face.layer.cornerRadius = UF(10);
        
        [self.face sd_setImageWithURL:[NSURL URLWithString:model.face] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        [self addSubview:self.face];
        
        self.name = [[UILabel alloc]initWithFrame:CGRectOf([self.face.ufo fx:1 ux:kMargin fy:0 uy:-kMargin/2], [self.ubo uw:kNameWight uh:60])];
        self.name.font = JKAppGlobleFontMenu;
        [self.name setTextColor:[UIColor blackColor]];
        self.name.text = model.name;
        [self addSubview:self.name];
        
        self.lastTalk = [[UILabel alloc]initWithFrame:CGRectOf([self.face.ufo fx:1 ux:kMargin fy:1 uy:-kTalkHight], [self.ubo fw:UB_WIDTH/2 uh:kTalkHight])];
        self.lastTalk.font = JKAppGlobleFontSmallTagText;
        [self.lastTalk setTextColor:[UIColor darkGrayColor]];
//        DLog(@"lastTalk = %@",model.lastTalk);
        if (model.lastTalk.length > 0 && ![model.lastTalk isEqualToString:@"0"]) {
            self.lastTalk.text = model.lastTalk;
        }else
        {
            self.lastTalk.text = @"[图片]";
        }
        
        [self addSubview:self.lastTalk];
        
        
        self.timeLab = [[UILabel alloc]initWithFrame:CGRectOf([self.ubo fx:1 ux:-kTimeWight-kMargin fy:0 uy:kMargin/2], [self.ubo uw:kTimeWight uh:kTimeHight])];
        self.timeLab.font = JKAppGlobleFontMinTagText;
        self.timeLab.textAlignment = NSTextAlignmentRight;
        [self.timeLab setTextColor:[UIColor darkGrayColor]];
        //需转化时间
       
         self.timeLab.text = [XCommon timeFromNowSinceDateline:model.time];
        [self addSubview:self.timeLab];
        
        for (int i = 0 ; i < tempPmids.count; i++) {
            
            if (self.pmid.integerValue == [[tempPmids objectAtIndex:i]integerValue] || [self.pmid isEqualToString:[tempPmids objectAtIndex:i]]) {
                
                self.redPoint = [[UIImageView alloc]initWithFrame:CGRectOf([self.face.ufo fx:1 ux:-kRedPointH/3 uy:-kRedPointH/3], [self.ubo uw:kRedPointH uh:kRedPointH])];
                self.redPoint.backgroundColor = [UIColor redColor];
                self.redPoint.layer.masksToBounds = YES;
                self.redPoint.layer.cornerRadius = self.redPoint.bounds.size.width/2;;
                [self addSubview:self.redPoint];
            }
        }

        
    
    
    
}

-(void)addCellRedPoint:(NSNotification*)info
{
    NSDictionary *pmidDictionary = [info userInfo];

//    DLog(@"%@",[pmidDictionary objectForKey:@"tempPmid"]);
    
    [self shouldReloadwithPmids:[pmidDictionary objectForKey:@"tempPmid"]];
}

-(void)shouldReloadwithPmids:(NSMutableArray*)array{
        
    if (self.delegate && [self.delegate respondsToSelector:@selector(reloadWithPmids:)]) {
        
        [self.delegate reloadWithPmids:array];
    }
    
}
     
@end

















